package StudyCode.summary.mycollection;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

/**
 * 版权所有 科技与人文(www.tah1986.com)
 */
public class CreatesAnArrayOfTheSpecifiedClassAndFillWithIterable {
	@SuppressWarnings("unchecked")
	public static <T> T[] arrayOf(Iterable<T> it, Class<T> clazz) {
		final List<T> list = listOf(it);
		final T[] result = (T[]) Array.newInstance(clazz, list.size());
		int i = 0;
		for (final T t : it) {
			result[i] = t;
			i++;
		}
		return result;
	}


	public static <T> List<T> listOf(Iterable<T> it) {
		if (it instanceof List) {
			return (List<T>) it;
		}
		final List<T> result = new LinkedList<T>();
		for (final T t : it) {
			result.add(t);

		}
		return result;
	}


	public static <T> List<T> listOf(T... items) {
		return Arrays.asList(items);
	}
}
